
var videoPlayer = null; //定义播放器

//初始化index
$(function () {
    
    //翻页加载更多--使用mescroll插件
    var mescroll = new MeScroll("mescroll", {
        down: {
            use: false,
            auto: false
        },
        up: {
            callback: getListData, //上拉回调,此处可简写; 相当于 callback: function (page) { getListData(page); }
            isBounce: false, //此处禁止ios回弹,解析(务必认真阅读,特别是最后一点): http://www.mescroll.com/qa.html#q10
            empty: {
                tip: "暂无相关数据~" //提示
            },
            page: {num: 0, size: 10, time: null},
            onScroll: scrollM,
            htmlNodata: '<p class="upwarp-nodata">没有更多了</p>'
        }
    })
    function getListData(page) {
        var pageIndex = page.num - 1; //改变起始page=0；
        getListDataFromNet(pageIndex, page.size, function (pageData) {
            //mescrollArr[dataIndex].endSuccess(pageData.length, hasNext); //必传参数(当前页的数据个数, 是否有下一页true/false)
            mescroll.endSuccess(pageData.length, hasNext);
            //设置列表数据
            setListData(pageData);
        }, function () {
            //联网失败的回调,隐藏下拉刷新和上拉加载的状态;
            mescroll.endErr();
        });
    }
    function getListDataFromNet(pageNum, pageSize, successCallback, errorCallback) {
        var data = {
            size: pageSize, //每页个数
            page: pageNum,  //页码
        }
        var basePar = initPar();
        $.extend(data, basePar);
        $.ajax({
            type: 'GET',
            url: website + 'wxcibn/getShortVideoList', //获取短视频列表接口
            async: true,   //注意！此处必须是异步的，否则阻塞DOM操作
            dataType: 'json',
            data: data,
            success: function (data) {
                var curPageData = data.data
                hasNext = (curPageData.length >= pageSize) ? true : false;
                successCallback(curPageData);
            },
            error: errorCallback
        });
    }
    
    function setListData(pageData) {
        for (var i = 0; i < pageData.length; i++) {
            var pd = pageData[i];
            var durationTime = initTimeLength(pd['endTime'] - pd['startTime']);
            var videolink = formatFileUrl(pd['programUrl'], pd['programUrl']);
            var icon = formatFileUrlImgCut(pd['contentImg'], pd['contentImg'], 1, 1);
            var link = encodeURI('detail.html?contentId=' + pd['contentId'] + '&keywords=' + pd['topicId']);
            var langLink = pd['programCibnId'] //列表页面 跳转全片的url
            var html = `
                <li class="oam-video_item">
                    <div class="oam-item">
                        <div class="oam-video-container" id=${pd['contentId']}  data-video-url=${videolink} data-video-poster=${icon}></div>
                        <img src=${icon} alt="" class="oam-video-poster">
                        <div class="oam-mark"></div>
                        <div class="oam-title">
                            <p>${pd['title']}</p>
                        </div>
                        <div class="oam-video-play" onclick="videoInit(this)">
                            <img src="img/play.png" alt="">
                        </div>
                    </div>
                    <div class="oam-item-bottom clearfix" data-id=${langLink} onclick="goFullfilm(this)">
                        <p class="oam-bottom-left">${durationTime}</p>
                        <div class="oam-bottom-right">
                            <div class="oam-full-film"  >
                                <img src="img/playFullList.png" alt="">看全片
                            </div>
                            <!--<div class="oam-share" onclick="weixinShare()">
                                <img src="img/shareList.png" alt="">分享
                            </div>-->
                        </div>
                    </div>
                </li>
            `
            $('#oam_dataList').append(html)
        }
    }
});

/*生成播放器*/
function videoInit(self) {
    if ($(self).parent().find('video').length > 0) {
        return;
    }
    if (videoPlayer) {
        videoPlayer.overVideo();
        $(".title_mark,.item_title,.video_center").show();
    }
    var container = $(self).parent().find('.oam-video-container');
    var id = container.attr('id');
    var url = container.attr('data-video-url');
    var poster = container.attr('data-video-poster');
    if (url) {
        videoPlayer = new createVideo(
            id,	//容器的id
            {
                url: url, 	//视频地址
                autoplay: true, //是否自动播放
                poster: poster
            }
        );
    }
};
/*超出视口初始化video*/
function scrollM() {
    var obj = document.getElementsByTagName("video").length;
    if (obj > 0) {
        var a = $("video").offset();
        var aH = $("video").height();
        var scroW = $(window).scrollTop();
        var heightW = $(window).height();
        if (a.top + aH < scroW || a.top > (scroW + heightW)) {
            swiperInit();
        }
    }
};
/*销毁播放器video*/
function swiperInit() {
    if ($(".oam-video-container").children().length >= 1) {
        $(".oam-video-container").empty();
        $(".oam-mark,.oam-title,.oam-video-play").show();
        $(".oam-video-poster").show();
    }
    ;
}
/*列表页面跳转看全片*/
function goFullfilm(obj) {
    var href = 'http://gaoqing.wx.cibn.cc/vod/detail/' + $(obj).attr('data-id') // 跳转长视频
    window.location.href = href
}

